<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="resources/css/style.css">
<script type="text/javascript">

	function update() {
		$("#usernameError").html("");
		$("#passwordError").html("");
		$("#vecchiaPasswordError").html("");

		if ($("#username").val() == "" || $("#username").val() == "undefined") {
			$("#usernameError").html("Username vuoto");
			$("#username").focus();
			return false;
		}
		if ($("#vecchiaPassword").val() == "" || $("#vecchiaPassword").val() == "undefined") {
			$("#vecchiaPasswordError").html("Password vuota");
			$("#vecchiaPassword").focus();
			return false;
		}
		if ($("#password").val() == "" || $("#password").val() == "undefined") {
			$("#passwordError").html("Password vuota");
			$("#password").focus();
			return false;
		}

		$.ajax({
			url : 'checkUsername.htm',
			type : "POST",
			data : ({
				username : $('#username').val()
			}),
			success : function(res) {
				if (res == "yes" && $('#username').val().match(/^\w+$/)) {
					$.ajax({
						url : "updateUser.htm",
						type : "post",
						data : ({
							username : $("#username").val(),
							vecchiaPassword: $("#vecchiaPassword").val(),
							password: $("#password").val()
						}),
						success : function(ris) {
							if (ris == "success")
								redir("welcomeUser.htm");
							else if (ris == "error")
								$("#error").html("<h2>Errore nella compilazione del form</h2>");
						}
					});
				} else if (res == "no") {
					$("#error").html("<h2>Username gi&agrave; presente!</h2>");
				}
			}
		});
	}
</script>
<title>Insert title here</title>
</head>
<body>
	<div class="box">
		<form method="POST" id="update">
			<table>
				<tr>
					<td>Username:</td>
					<td><input id="username" name="username" type="text" onfocus="$('#error').html('');" onkeydown="$('#error').html('');$('#usernameError').html('');"/></td>
					<td id="usernameError"></td>
				</tr>
				<tr>
					<td>Vecchia Password:</td>
					<td><input id="vecchiaPassword" name="vecchiaPassword" type="password" onfocus="$('#error').html('');" onkeydown="$('#error').html('');$('#vecchiaPasswordError').html('');"/></td>
					<td id="vecchiaPasswordError"></td>
				</tr>
				<tr>
					<td>Password:</td>
					<td><input id="password" name="password" type="password" onfocus="$('#error').html('');" onkeydown="$('#error').html('');$('#passwordError').html('');"/></td>
					<td id="passwordError"></td>
				</tr>
				<tr>
					<td><input type="button" value="Modifica" onclick="update()"></td>
					<td><input type="reset" value="Annulla" onclick="redir('welcomeUser.htm')"></td>
				</tr>
			</table>
		</form>
		<a href="#" onclick="redir('deleteUser.htm')">Elimina il mio account!</a>
		<div id="error"></div>
	</div>
</body>
</html>